<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IResourceAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';

	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];

	class   MySqlResourceAccessDao implements IResourceAccessDao {
  
		/**
		 * Constructor
		 */
		 public function __construct () {}
		 
		 /**
		  * Creates a new resource
		  */
		 public function createResource($resource) {
			$category = $resource -> getCategory();
			$identifier = $resource -> getIdentifier();
			$specifications = $resource -> getSpecifications();
			$cost = $resource -> getCost();
			
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "SELECT MAX(pk_number) * -1 pk_number FROM REQUEST";			
			$sentence = mysql_query($query, $conn);			
			$row = mysql_fetch_array($sentence);
			$requestNumber = $row['pk_number'];
			
			$query = "SELECT MAX(pk_control_number) * -1 pk_control_number FROM REQUEST";
			$sentence = mysql_query($query, $conn);			
			$row = mysql_fetch_array($sentence);
			$requestControlNumber = $row['pk_control_number'];
			
			switch ($category) {
				case 'EMPLEADOS':
					$query = "INSERT INTO RESOURCE 
							  (category, specifications, weekly_salary, fk_number_request, 
							      fk_control_number_request, fk_id_number_employee) 
							  VALUES ('$category', '$specifications', $cost, $requestNumber,
							  	  $requestControlNumber, $identifier)";
				break;
				
				default:
					$query = "INSERT INTO RESOURCE
							  (category, name, specifications, cost, fk_number_request, 
							  	  fk_control_number_request)
							  VALUES ('$category', '$identifier', '$specifications', $cost, 
							  	  $requestNumber, $requestControlNumber)";
				break;
			}
			
			$sentence = mysql_query($query, $conn);
				
			$connection -> closeConnection($conn);
			
			$GLOBALS['debug'] -> log($query, "query");
			
			if ($sentence)
				return 1;
			
			return 0;
		 }
	 
	  /**
		 * Selects all Resource
		 		 */
		public function selectResources() {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			$GLOBALS['debug']->log('Inicio del DAO');
			
					$query = "SELECT pk_id,first_name, first_last_name,name,category,cost
							  FROM RESOURCE,EMPLOYEE,REQUEST
							  WHERE 
							 `fk_id_number_employee`=pk_id_number and 
							 `fk_number_request`=pk_number and 
							 `fk_control_number_request` =pk_control_number";			
			
			$sentence = mysql_query($query, $conn);
				
			$conn = $connection -> closeConnection($conn);
				$GLOBALS['debug']->log($query,'Funcion DAO');
			return $sentence;
		 }
		 
	 
  
  }


?>